"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.
"""

"""
Case Type   : 资源池化
Case Name   : 资源池化场景下,备机prepare语句对全局临时表的数据同步
Create At   : 2025-03-07
Owner       : zhongchengzhi
Description :
    1、查询集群状态
    2、主机创建全局临时表并插入数据
    3、备机准备prepare语句
    4、主机增加列
    5、备机执行prepare语句
    6、主机删除列
    7、备机执行prepare语句
    8、清理数据、恢复集群状态

Expect      :
    1、查询结果正常
    2、创建、插入数据成功
    3、创建物化视图成功
    4、修改数据成功
    5、刷新物化视图成功
    6、查询物化视图结果正常
    7、查询物化视图结果正常
    8、清理数据、恢复集群状态成功
History     :
    1、2025-03-07 创建
"""
import os
import unittest
import time

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro

Primary_ROOTSH = CommonSH('PrimaryRoot')
Primary_SH = CommonSH('PrimaryDbUser')
Standby1_SH = CommonSH('Standby1DbUser')

@unittest.skipIf(1 == Primary_SH.get_node_num(), '单机环境不执行')
class SharedStorage(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.s1_comsh = CommonSH('Standby1DbUser')

    def test_shared_storage(self):
        text = '-----step1:查询数据库集群状态-----'
        self.log.info(text)
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cvipd')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)

        text = '-----step2:主机创建全局临时表;expect:创建全局临时表成功-----'
        self.log.info(text)
        sql = f'''
                drop table if exists temp_table_alter_010;
                create global temporary table temp_table_alter_010(
                c_id int,
                c_integer integer,
                c_real real,
                c_float float,
                c_double binary_double,
                c_decimal decimal(38),
                c_number number(38),c_number1 number,
                c_number2 number(20,10),
                c_numeric numeric(38),
                c_char char(50) default null,
                c_varchar varchar(20),
                c_varchar2 varchar2(4000),
                c_clob clob,
                c_raw raw(20),
                c_blob blob,
                c_date date,
                c_timestamp timestamp
                )on commit preserve rows;
                '''
        self.log.info(sql)
        status = Primary_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('CREATE TABLE', status, '执行失败' + text)

        text = '-----step3:备机准备和执行prepare语句;expect:执行成功-----'
        self.log.info(text)
        sql = f'''
                prepare standby1 as select * from temp_table_alter_010;
                explain execute standby1(1);
                '''
        self.log.info(sql)
        status = Standby1_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('Seq Scan on temp_table_alter_010  (cost=0.00..10.90 rows=90 width=876)', status, '执行失败' + text)

        text = '-----step4:主机增加列 expect:增加成功-----'
        self.log.info(text)
        sql = f'''
                alter table temp_table_alter_010 add(c_1 int);
                alter table temp_table_alter_010 add(c_2 BLOB);
                alter table temp_table_alter_010 add(c_3 varchar(200));
                alter table temp_table_alter_010 add(c_4 clob);
                '''
        self.log.info(sql)
        status = Primary_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('ALTER TABLE\nALTER TABLE\nALTER TABLE\nALTER TABLE', status, '执行失败' + text)

        text = '-----step5:备机执行prepare语句;expect:执行报错-----'
        self.log.info(text)
        sql = f'''
                prepare standby1 as select * from temp_table_alter_010;
                explain execute standby1(1);
                '''
        self.log.info(sql)
        status = Standby1_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('Seq Scan on temp_table_alter_010  (cost=0.00..10.75 rows=75 width=1062)\n(1 row)', status, '执行失败' + text)

        text = '-----step6:主机删除新增加的列;expect:执行成功-----'
        self.log.info(text)
        sql = f'''
                alter table temp_table_alter_010 drop c_1;
                alter table temp_table_alter_010 drop c_2;
                alter table temp_table_alter_010 drop c_3;
                '''
        self.log.info(sql)
        status = Primary_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('ALTER TABLE\nALTER TABLE\nALTER TABLE', status, '执行失败' + text)

        text = '-----step7:备机执行prepare语句;expect:执行报错-----'
        self.log.info(text)
        sql = f'''
                prepare standby1 as select * from temp_table_alter_010;
                explain execute standby1(1);
                '''
        self.log.info(sql)
        status = Standby1_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('Seq Scan on temp_table_alter_010  (cost=0.00..10.87 rows=87 width=908)\n(1 row)', status, '执行失败' + text)

        text = '-----step8:清理环境;expect:成功-----'
        self.log.info(text)
        sql = f'''
                drop table if exists temp_table_alter_010;
                '''
        self.log.info(sql)
        status = Primary_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('DROP TABLE', status, '执行失败' + text)

    def tearDown(self):
        text = '-----step9:恢复环境为初始集群状态; expect:成功-----'
        self.log.info(text)
        status = Primary_SH.exec_cm_ctl(mode='stop')
        self.log.info(status)
        self.assertIn('cm_ctl: stop cluster successfully.', status, '执行失败' + text)

        status = Primary_SH.exec_cm_ctl(mode='start')
        self.log.info(status)
        self.assertIn('cm_ctl: start cluster successfully.', status, '执行失败' + text)

        status = Primary_SH.exec_cm_ctl(mode='switchover', param='-a')
        self.log.info(status)
        self.assertIn(self.constant.cm_switchover_success_msg, status, '执行失败' + text)

        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cvipd')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')